package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.GamersCom.db.Game;
import com.GamersCom.db.Platform;
import com.GamersCom.model.GameMgr;
import com.GamersCom.model.GenreMgr;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.PlatformMgr;

/**
 * Servlet implementation class ListGames
 */
public class GamesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MgrFactory factory = (MgrFactory) this.getServletContext()
				.getAttribute("mgrFactory");
		PlatformMgr pfMgr = factory.getPlatformMgr();
		GenreMgr genreMgr = factory.getGenreMgr();
		GameMgr gameMgr = factory.getGameMgr();
		
		//retrieve the parameter
		String filter = request.getParameter("filter");
		
		try
		{
			List<Platform> pfList = pfMgr.getAll();
			request.setAttribute("pfList", pfList);	

System.out.println("filter : "+filter);	
			
			//get game by platform
			List<Game> gameList;
			if ( filter == null)
			{
				gameList = gameMgr.getAllGames();
			} else if ( filter.equals("all"))
			{
				gameList = gameMgr.getAllGames();
			}
			else 
			{			
				gameList = gameMgr.getAllByPf(filter);
			}
			request.setAttribute("gameList", gameList);
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
						
		String nextView = "/games.jsp";
		request.getRequestDispatcher(nextView).forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
